# 直接读取ui文件，假设我的ui文件名为"SL_manage.ui"，那么我的python代码应该是:
#MainUi.py
from PyQt5 import QtWidgets, uic,QtCore,QtGui
import os
from db import *

path = os.getcwd()
qtCreatorFile = path +os.sep+"window.ui"  # Enter file here.
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
#_translate = QtCore.QCoreApplication.translate#
_translate = QtCore.QCoreApplication.translate   #这个主要用于后期的UI界面文字字体以及颜色便捷的更改
# 创建一个对象，该对象就是你的整个窗体：
class mainui(QtWidgets.QMainWindow, Ui_MainWindow):
#这里的第一个变量是你该窗口的类型，第二个是该窗口对象。
#这里是主窗口类型。所以设置成当QtWidgets.QMainWindow。
#你的窗口是一个会话框时你需要设置成:QtWidgets.QDialog
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.i=10


        self.add.clicked.connect(self.AddVariable)
        self.delete_2.clicked.connect(self.DeleteVariable)
        self.upgrade.clicked.connect(self.UpdateVariable)
        self.last_one.clicked.connect(self.LastOne)
        self.next_one.clicked.connect(self.NextOne)
        self.sys_description_upgrade.clicked.connect(self.SysDescriptionUpgrade)
        # self.fuyang.toggle()
        self.fuyang.toggled.connect(self.choose_system)
        # self.Display(2)

        self.gunzhuan.toggled.connect(self.choose_system)
        self.pianhang.toggled.connect(self.choose_system)

    def choose_system(self):
        if self.fuyang.isChecked():
            self.i=2
            self.Display()

    # def choose_system2(self):
        if self.gunzhuan.isChecked():
            self.i=0
            self.Display()

    # def choose_system3(self):
        if self.pianhang.isChecked():
            self.i=1
            self.Display()




    def AddVariable(self):
        db = conndb()
        print("连接成功")
        sel='select MAX(ID) from VARIABLE'
        rs = SelectDB(db, sel)
        print(rs)
        id=str(int(rs[0][0])+1)
        print(id)
        sysid = self.sys_id.toPlainText()
        variableTypeId = self.va_type_id.toPlainText()
        variableName = self.va_name.toPlainText()
        variableType=self.va_type.toPlainText()
        variableDescription = self.va_description.toPlainText()
        inst = "insert into VARIABLE values("+"'"+id+"'"+','+"'"+sysid+"'"+','+"'"+variableTypeId+"'"+','+"'"+variableName+"'"+','+"'"+variableDescription+"'"+')'
        print(inst)
        DMLDB_N(db, inst)
    #
    #
    def DeleteVariable(self):
        db = conndb()
        print('连接成功')
        variableName=self.va_name.toPlainText()
        print(variableName)
        delt = 'delete from VARIABLE where VALUE_NAME='+"'"+variableName+"'"
        print(delt)
        DMLDB_N(db, delt)
    #
    #
    def UpdateVariable(self):
        db = conndb()
        print('连接成功')
        variableTypeId=self.va_type_id.toPlainText()
        variableName=self.va_name.toPlainText()
        #variableType=self.va_type.toPlainText()
        variableDescription=self.va_description.toPlainText()
        upgr = "UPDATE VARIABLE SET TYPE_ID="+"'"+variableTypeId+"'"+','+"DESCRIPTION="+"'"+variableDescription+"'"+" WHERE VALUE_NAME="+"'"+variableName+"'"
        print(upgr)
        DMLDB_N(db, upgr)
    #
    def LastOne(self):
        self.i -= 3
        self.Display()
    #
    #
    def NextOne(self):
        #SystemId = self.sys_id.toPlainText()


        self.i += 3
        self.Display()
    #
    #
    def SysDescriptionUpgrade(self):
        db = conndb()
        print('连接成功')
        sysDes=self.sys_description.toPlainText()
        print(sysDes)
        sysid=self.sys_id.toPlainText()
        print(sysid)

        upgr = "UPDATE CONTROLSYSTEM SET DESCRIPTION="+"'"+sysDes+"'" +"WHERE ID="+"'"+sysid+"'"
        # print(upgr)
        DMLDB_N(db, upgr)
        # print('改好了')

    def Display(self):

        print("============== 连接数据库 ==================")
        db = conndb()
        print("连接成功！")
        sel = 'select * from CONTROLSYSTEM,VARIABLE,TYPE where CONTROLSYSTEM.ID=VARIABLE.CONTROLSYSTEM_ID and VARIABLE.TYPE_ID=TYPE.ID'
        # sel = 'select * from CONTROLSYSTEM'
        rs = SelectDB(db, sel)
        print("ok")

        self.sys_id.setText(rs[self.i][0])
        self.sys_description.setText(rs[self.i][2])  # xitongmiaoshu
        self.va_name.setText(rs[self.i][6])  # bianliangming
        self.va_type.setText(rs[self.i][9])  # bianliangleixing
        self.va_type_id.setText(rs[self.i][8])  # bianliangleixingdaihao
        self.va_description.setText(rs[self.i][7])  # bianliangmiaoshu


#注意每一个函数开始都应该调用连接数据库的函数
#调用函数的时候注意前面加不加self.
#sql语句写的时候注意引号问题
#三个QradioButton应该放在一个Group Box中，以实现互斥